/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelModificarCandidato.java
 *
 * Created on Apr 9, 2011, 8:54:33 PM
 */
package frontera.interfaz.panel;

import control.SIRHApp;
import control.accion.EmpleadoAdministrar;
import control.comparaciones.ComparacionContenencia;
import control.comportamiento.EscucharBusqueda;
import control.filtros.FiltroCargosEstado;
import entidad.Candidato;
import entidad.Cargo;
import frontera.interfaz.panel.herramientas.PanelHerramientasBuscar;
import java.util.ArrayList;
import javax.swing.Action;
import javax.swing.JOptionPane;

/**
 *
 * @author tuareg
 */
public class PanelModificarCandidato extends javax.swing.JPanel implements EscucharBusqueda<Cargo> {

    /** Creates new form PanelModificarCandidato */
    public PanelModificarCandidato(Candidato c) {
        initComponents();
        inicializarVariables(c);
    }

    private void inicializarVariables(Candidato c) {
        nombreTF.setText(c.getNombre());
        labelCandidato.setText(c.getNombre());
        cedulaTF.setText(String.valueOf(c.getCedula()));
        profesionTF.setText(c.getProfesion());
        aspiracionSalarialTF.setText(String.valueOf(c.getAspiracionSalarial()));
        candidato = c;
        cargoTF.setText(c.getAspiracionCargo().getNombre());
        cargoAsociado = c.getAspiracionCargo();
        comboEstado.setSelectedItem(c.getEstado());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        cedulaTF = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        profesionTF = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        aspiracionSalarialTF = new javax.swing.JFormattedTextField();
        jPanel6 = new javax.swing.JPanel();
        buscarCargoB = new javax.swing.JButton();
        cargoTF = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        comboEstado = new javax.swing.JComboBox();
        cancelarB = new javax.swing.JButton();
        guardarB = new javax.swing.JButton();
        labelCandidato = new javax.swing.JLabel();
        labelResultado = new javax.swing.JLabel();

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/UpdCandidato.png"))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 32));
        jLabel2.setText("Actualizando Candidato");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Básicos"));
        jPanel1.setLayout(new java.awt.GridLayout(2, 1));

        jLabel3.setText("Nombre completo");

        nombreTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nombreTFKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(26, 26, 26)
                .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel1.add(jPanel8);

        jPanel7.setLayout(new java.awt.GridLayout(1, 2));

        jLabel4.setText("Cédula");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cedulaTF, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                    .addComponent(cedulaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel7.add(jPanel3);

        jLabel5.setText("Profesion");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(profesionTF, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(profesionTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel7.add(jPanel4);

        jPanel1.add(jPanel7);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Contratación"));
        jPanel2.setLayout(new java.awt.GridLayout(2, 2));

        jLabel6.setText("Aspiracion salarial");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aspiracionSalarialTF, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(aspiracionSalarialTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel5);

        buscarCargoB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/search.png"))); // NOI18N
        buscarCargoB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarCargoBActionPerformed(evt);
            }
        });

        cargoTF.setEditable(false);

        jLabel8.setText("Cargo al que aspira");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cargoTF, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buscarCargoB, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(buscarCargoB, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(cargoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel6);

        jLabel7.setText("Estado :");

        comboEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pendiente", "Seleccionado", "Para entrevista", "Descartado" }));

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(comboEstado, 0, 248, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(comboEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.add(jPanel9);

        cancelarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Cancelar.png"))); // NOI18N
        cancelarB.setText("Cancelar");
        cancelarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBActionPerformed(evt);
            }
        });

        guardarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Save.png"))); // NOI18N
        guardarB.setText("Guardar");
        guardarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarBActionPerformed(evt);
            }
        });

        labelCandidato.setFont(new java.awt.Font("Dialog", 0, 22));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(labelCandidato, javax.swing.GroupLayout.PREFERRED_SIZE, 424, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(labelResultado, javax.swing.GroupLayout.DEFAULT_SIZE, 676, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(440, Short.MAX_VALUE)
                        .addComponent(guardarB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelarB)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelCandidato, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelResultado, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guardarB)
                    .addComponent(cancelarB))
                .addContainerGap(22, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void guardarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarBActionPerformed

        
        String resultadoOperacion = SIRHApp.getInstance().getAdmCandidatos().actualizarCandidato(candidato, nombreTF.getText(), cedulaTF.getText(),
                profesionTF.getText(), aspiracionSalarialTF.getText(), (String) comboEstado.getSelectedItem(), cargoAsociado);

        if (resultadoOperacion.equals("")) {

            SIRHApp.getInstance().getVentanaPrincipal().setPanelReclutamientoPrincipal(SIRHApp.getInstance().getVentanaPrincipal().getPanelCandidatos());
            //Dejar la búsqueda del panel anterior en blanco
            ((PanelHerramientasBuscar<Candidato>) SIRHApp.getInstance().getFactoryInterfaz().getPanelCandidatos().getPanelHerramientas()).inicializarBusqueda();
            if ((String) comboEstado.getSelectedItem() == "Seleccionado") {
                JOptionPane.showMessageDialog(SIRHApp.getInstance().getVentanaPrincipal(), "A continuación se le redirigirá a la pestaña de empleados \n"
                        + "para que llene los datos del nuevo empleado seleccionado.", "Nuevo Empleado", JOptionPane.INFORMATION_MESSAGE);
                SIRHApp.getInstance().getAdmCandidatos().eliminarCandidato(candidato);
                Action a = new EmpleadoAdministrar();
                a.actionPerformed(null);
                SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalEmpleado(new PanelCrearEmpleado(nombreTF.getText(), cedulaTF.getText(),
                        profesionTF.getText(), cargoAsociado));
            }
            SIRHApp.getInstance().getVentanaPrincipal().getPanelCandidatos().actualizarLista(SIRHApp.getInstance().getSistema().getCandidatos());


        } else {
            labelResultado.setText(resultadoOperacion);
        }

    }//GEN-LAST:event_guardarBActionPerformed

    private void cancelarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBActionPerformed
        SIRHApp.getInstance().getVentanaPrincipal().setPanelReclutamientoPrincipal(
                SIRHApp.getInstance().getVentanaPrincipal().getPanelCandidatos());
        ((PanelHerramientasBuscar<Candidato>) SIRHApp.getInstance().getFactoryInterfaz().getPanelCandidatos().getPanelHerramientas()).inicializarBusqueda();
    }//GEN-LAST:event_cancelarBActionPerformed

    private void nombreTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nombreTFKeyReleased
        labelCandidato.setText(nombreTF.getText());
    }//GEN-LAST:event_nombreTFKeyReleased

    private void buscarCargoBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarCargoBActionPerformed
        ArrayList<Cargo> cargos = SIRHApp.getInstance().getAdmCargos().buscar(new FiltroCargosEstado(), new ComparacionContenencia(), "Vacante");
        SIRHApp.getInstance().getVentanaPrincipal().setPanelReclutamientoPrincipal(SIRHApp.getInstance().getFactoryInterfaz().getPanelBusquedaCargo(this, this, this.getParent(), cargos));

    }//GEN-LAST:event_buscarCargoBActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField aspiracionSalarialTF;
    private javax.swing.JButton buscarCargoB;
    private javax.swing.JButton cancelarB;
    private javax.swing.JTextField cargoTF;
    private javax.swing.JFormattedTextField cedulaTF;
    private javax.swing.JComboBox comboEstado;
    private javax.swing.JButton guardarB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JLabel labelCandidato;
    private javax.swing.JLabel labelResultado;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JFormattedTextField profesionTF;
    // End of variables declaration//GEN-END:variables
    private Candidato candidato;
    private Cargo cargoAsociado;

    public void procesarResultadoBusqueda(Cargo resultado) {
        cargoAsociado = resultado;
        cargoTF.setText(resultado.getNombre());
        SIRHApp.getInstance().getVentanaPrincipal().setPanelReclutamientoPrincipal(this);
    }
}
